package org.travel_system.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.travel_system.pojo.EmergencyInformation;
import org.travel_system.service.EmergencyService;
import org.travel_system.utils.Result;

@RestController
@RequestMapping("/emergencies")
public class EmergencyController
{
    private final EmergencyService emergencyService;

    @Autowired
    public EmergencyController(EmergencyService emergencyService)
    {
        this.emergencyService = emergencyService;
    }

    @GetMapping
    public Result<?> getEmergencies()
    {
        return Result.success(emergencyService.getEmergencies());
    }

    @GetMapping("/{id}")
    public Result<?> getEmergencyById(@PathVariable int id)
    {
        return Result.success(emergencyService.getEmergencyById(id));
    }

    @GetMapping("/approved")
    public Result<?> getEmergenciesApproved()
    {
        return Result.success(emergencyService.getEmergenciesApproved());
    }

    @PostMapping
    public Result<?> addEmergency(@RequestBody EmergencyInformation emergencyInformation)
    {
        return emergencyService.addEmergency(emergencyInformation) > 0 ?
                Result.success() : Result.error("增加应急信息失败");
    }

    //审批
    @PatchMapping("/{id}")
    public Result<?> approveEmergency(@PathVariable int id)
    {
        return Result.success(emergencyService.approveEmergency(id));
    }
}
